package com.ihd.ihardware.view.tzc.health.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemExpReportBinding;
import com.ihd.ihardware.pojo.ReportRes;
import com.ihd.ihardware.view.tzc.health.model.ReportRepository;
import com.ihd.ihardware.view.tzc.health.view.ReportActivity;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class CountHisViewModel extends AndroidViewModel {

    private ReportRepository mReportRepository;

    public CountHisViewModel(@NonNull Application application) {
        super(application);
    }

    private ObservableArrayList<ReportRes.DataBean> mConcerned = new ObservableArrayList<>();
    public CommonAdapter mConcernedAdapter = new CommonAdapter<ReportRes.DataBean, ItemExpReportBinding>(R.layout.item_exp_report, mConcerned, null) {
        @Override
        public void convert(ItemExpReportBinding mDataBinding, ReportRes.DataBean test_concern, int position) {
            mDataBinding.concernedItemLL.setShowMode(SwipeLayout.ShowMode.LayDown);
            mDataBinding.mainLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), ReportActivity.class);
                    i.putExtra("report", mConcerned.get(position));
                    v.getContext().startActivity(i);
                }
            });
            mDataBinding.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delWeighing(mConcerned.get(position).getId());
                }
            });
            setView(mDataBinding.statusTV, test_concern.getBmiDes());
        }
    };

    private void setView(TextView statusTV, String content) {
        if (TextUtils.isEmpty(content)) {
            statusTV.setVisibility(View.GONE);
        } else {
            statusTV.setVisibility(View.VISIBLE);
            statusTV.setText(content);
            if ("瘦".equals(content)) {
                statusTV.setBackgroundResource(R.drawable.corners_tag_0);
            } else if ("偏瘦".equals(content)) {
                statusTV.setBackgroundResource(R.drawable.corners_tag_1);
            } else if ("标准".equals(content)) {
                statusTV.setBackgroundResource(R.drawable.corners_tag_2);
            } else if ("偏胖".equals(content)) {
                statusTV.setBackgroundResource(R.drawable.corners_tag_3);
            } else if ("肥胖".equals(content)) {
                statusTV.setBackgroundResource(R.drawable.corners_tag_4);
            } else if ("极度肥胖".equals(content)) {
                statusTV.setBackgroundResource(R.drawable.corners_tag_6);
            }
        }
    }

    public void setReport(List<ReportRes.DataBean> rd) {
        mConcerned.clear();
        if (rd != null && rd.size() > 0)
            mConcerned.addAll(rd);
    }

    public void reports(String familyId, String type) {
        if (mReportRepository == null)
            mReportRepository = new ReportRepository();
        mReportRepository.reports(familyId, type);
    }

    public void delWeighing(String id) {
        if (mReportRepository == null)
            mReportRepository = new ReportRepository();
        mReportRepository.delWeighing(id);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mReportRepository) {
            mReportRepository.onDestroy();
            mReportRepository = null;
        }
    }
}
